package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.hnapay.ebank.HnaPayMessage;
import com.gopay.common.util.MoneyUtil;

/**
 * 新生在线后台银行回应
 * 
 * @HnaPayServerRes.java
 * @author LILILI
 */
@Controller
@Scope("prototype")
public class HnaPayServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(HnaPayServerRes.class);

	@RequestMapping(value = "/hnaPayServerRes")
	protected ModelAndView execute(HttpServletRequest request,
			HttpServletResponse response) {
		logger.error("--------------- HnaPayServerRes bankback  ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		HnaPayMessage message = new HnaPayMessage();
		message.setBank(BankConstants.BANK_LOGO_HNAPAY);

		String orderID = request.getParameter("orderID");
		String stateCode = request.getParameter("stateCode");
		String resultCode = request.getParameter("resultCode");
		String orderAmount = request.getParameter("orderAmount");
		String payAmount = request.getParameter("payAmount");
		String acquiringTime = request.getParameter("acquiringTime");
		String completeTime = request.getParameter("completeTime");
		String orderNo = request.getParameter("orderNo");
		String partnerID = request.getParameter("partnerID");
		String remark = request.getParameter("remark");
		String charset = request.getParameter("charset");
		String signType = request.getParameter("signType");
		String signMsg = request.getParameter("signMsg");

		// 生产HNAPAY平台验签对象
		StringBuffer sb = new StringBuffer();
		sb.append("orderID=").append(orderID);
		sb.append("&resultCode=").append(resultCode);
		sb.append("&stateCode=").append(stateCode);
		sb.append("&orderAmount=").append(orderAmount);
		sb.append("&payAmount=").append(payAmount);
		sb.append("&acquiringTime=").append(acquiringTime);
		sb.append("&completeTime=").append(completeTime);
		sb.append("&orderNo=").append(orderNo);
		sb.append("&partnerID=").append(partnerID);
		sb.append("&remark=").append(remark);
		sb.append("&charset=").append(charset);
		sb.append("&signType=").append(signType);
		message.setOrderId(orderID);
		message.setBankOrderId(orderID);
		message.setAmount(new BigDecimal(payAmount));
		message.setStateCode(stateCode);
		message.setPartnerID(partnerID);
		message.setPlainMessage(sb.toString());
		message.setSignMsg(signMsg);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		logger.error("组装待签名字符串：" + sb.toString());
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		HnaPayMessage message = (HnaPayMessage) result.getModel("res");
		String retCode = request.getParameter("stateCode");
		OrderInfo orderInfo = new OrderInfo();
		String amount = request.getParameter("orderAmount");
		if (StringUtils.isNotBlank(amount)) {
			amount = MoneyUtil.formatAmt(amount);
		}
		if (message != null) {
			if (retCode != null && retCode.equals("00")) {
				orderInfo.setOrderId(message.getOrderId());
				orderInfo.setAmt(amount);
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("00");
				orderInfo.setRetCode("00");
				orderInfo.setRetMsg("");
			} else {
				orderInfo.setOrderId(message.getOrderId());
				orderInfo.setAmt(amount);
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("01");
				orderInfo.setRetCode("01");
				orderInfo.setRetMsg("");
			}
		}
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("orderID"))
				|| StringUtils.isEmpty(request.getParameter("stateCode"))) {
			return false;
		}
		return true;
	}

}
